<script setup>
import {onBeforeMount, ref} from 'vue';
import {useRouter} from 'vue-router';
import FooterNav from "@/components/footerNav.vue";
import {showToast} from "vant";
import {useUserStore} from "@/store/user.js";
import axios from "@/api/index.js";
import {getUserInfo} from "@/api/request.js";

const userInfo = ref({
  username: '',
  avatar: '',
  intro: ''
})
const router = useRouter();
const store = useUserStore()

onBeforeMount(async () => {
  await getUserInfo({
    userId: store.userId
  }).then(res => {
    userInfo.value = res.data
    console.log(userInfo.value)
  })
})


function logout() {
  // 这里可以添加退出登录的逻辑，例如清除本地存储的token
  // 然后导航到登录页面
  store.userId = 0
  store.isAuthenticated = false

  showToast('退出登录')
  router.push('/login');
}
</script>

<template>
  <van-nav-bar title="我的"/>

  <div class="app-container">
    <div class="profile-header">
      <div class="user-avatar">
        <van-image :src='`${axios.getUri()}/${userInfo.avatar}`' height="5rem" round width="5rem"/>
      </div>
      <div class="center">
        <div class="username">{{ userInfo.username }}</div>
        <div class="user-intro">{{ userInfo.intro }}</div>
      </div>
      <div class="right">
        <van-icon name="records-o" size="1.5rem" color="#999999" @click="router.push('/modify-user-info')" />
      </div>
    </div>

    <div class="user-center">
      <van-cell is-link title="我的知识库" to="/knowledge-base"/>
      <van-cell is-link title="我的文章" to="/my-article"/>
      <van-cell is-link title="文章收藏" to="/collect"/>
    </div>

    <div class="logout">
      <van-button block type="danger" @click="logout">退出登录</van-button>
    </div>
  </div>


  <FooterNav/>
</template>


<style lang="scss" scoped>
.app-container {
  margin-top: .5rem;
  padding: 0 .5rem;

  & > div {
    margin-bottom: 1rem;
  }
}


.profile-header {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  background-color: #ffffff;
  width: 100%;
  height: fit-content;
  padding: 1rem 0.5rem;
  box-sizing: border-box;

  .user-avatar {
    width: 5rem;
    height: 5rem;
    border-radius: 50%;
    border: 3px solid #bfbcff;
    overflow: hidden;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  }

  .center {
    flex: 1;
    padding: 1rem;

    .username {
      font-size: 1.5rem;
      color: #333333;
      font-weight: bold;
      margin-bottom: .5rem;
      line-height: 1.5;
    }

    .user-intro {
      font-size: 1rem;
      color: #999999;
      line-height: 1.5;
    }
  }
  .right {
    width: 2rem;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>